﻿using System;
using System.Data;
using System.IO;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class ExamUISet : System.Web.UI.Page
    {
        private ActivityBLL m_ActivityBll = new ActivityBLL();
        private EnterpriseUserBLL m_EnterpriseUserBll = new EnterpriseUserBLL();

        private string m_UserName = string.Empty;
        private string m_EnterpriseId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteEnterpriseMaster)Master).InitMenu();
            ((SiteEnterpriseMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteEnterpriseMaster)Master).b_hmenu1.Style.Clear();
            ((SiteEnterpriseMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu4.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).s_menu1_4.Style.Add("COLOR", "#147ab8");
            ((SiteEnterpriseMaster)Master).s_menu1_4.Style.Add("TEXT-DECORATION", "none");  

            /*判断是否是企业用户身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../Enterprise/EnterpriseLogin.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    m_UserName = Session["username"].ToString();
                    m_EnterpriseId = m_EnterpriseUserBll.GetEnterpriseIdByUser(m_UserName);
                    DataSet dsActivity = m_ActivityBll.GetActivityAll(m_EnterpriseId);

                    if (dsActivity != null && dsActivity.Tables[0].Rows.Count > 0)
                    {
                        ddlActivity.DataSource = dsActivity.Tables[0].DefaultView;
                        ddlActivity.DataValueField = "activity_id";
                        ddlActivity.DataTextField = "activity_name";
                        ddlActivity.DataBind();

                        bind();
                    }
                }
            }
        }

        private void bind()
        {
            int aID = Int32.Parse(ddlActivity.SelectedValue);

            txtWelcome.Text = m_ActivityBll.GetModel(aID).Activity_welcometext;
            txtCompanyIntroduce.Text = m_ActivityBll.GetModel(aID).Activity_companyintrotext;
            txtbActivityIntroduce.Text = m_ActivityBll.GetModel(aID).Activity_content;

            logoIMG.ImageUrl = m_ActivityBll.GetModel(aID).Activity_logo;
        }

        private string uploadImg(string filePath)
        {
            try
            {
                string fileNewName = Guid.NewGuid().ToString().Substring(0, 8) + filePath;
                string uploadfilename = Server.MapPath("~/userfiles") + "\\" + fileNewName;

                if (!File.Exists(uploadfilename))
                {
                    fulLogo.SaveAs(uploadfilename);
                }

                return fileNewName;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return string.Empty;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Activity temp = new Activity();

            temp.Activity_id = int.Parse(ddlActivity.SelectedValue);

            temp.Activity_content = txtbActivityIntroduce.Text;
            temp.Activity_welcometext = txtWelcome.Text;
            temp.Activity_companyintrotext = txtCompanyIntroduce.Text;
            
            temp.Activity_logo = logoIMG.ImageUrl;

            if (fulLogo.HasFile)
            {
                string fileSrv = uploadImg(fulLogo.FileName);
                temp.Activity_logo = fileSrv;
            }

            m_ActivityBll.Update(temp);
            MessageBox.ShowAndRedirect(this, "测评页面定制成功", "ManageActivity.aspx");
        }

        protected void btnPass_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageActivity.aspx");
        }

        protected void ddlActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string fileSrv = uploadImg(fulLogo.FileName);
            logoIMG.ImageUrl = "~/userfiles" + "\\" + fileSrv;
        }
    }
}